#!/usr/bin/env python
'''
Copyright (C) 2006, 2007 Aaron Spike, aaron@ekips.org
Copyright (C) 2007 Michael Greb

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'''

import pygame, math
from pygame.locals import *
from activity import *

class MenuItem(pygame.sprite.Sprite):
    def __init__(self, image, activity):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image(image)
        self.activity = activity
        self.pos = (-self.rect.width,-self.rect.height)
    def run(self, screen):
        self.activity(screen).run()
    def place(self, x, y):
        self.pos = (x, y)
        self.rect.center = self.pos

class Finger(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) 
        self.image, self.rect = load_image('finger.png')
    def update(self):
        pos = pygame.mouse.get_pos()
        self.rect.midtop = pos

class MenuActivity(Activity):
    def __init__(self, screen, items):
        Activity.__init__(self, screen)
        
        self.activities = pygame.sprite.RenderPlain()
        
        w = self.screen.get_width() / 2.0
        h = self.screen.get_height() / 2.0
        magnitude = 0.8 * min(w, h)
        
        if items:
            rotate = 360.0 / len(items)
            for i in range(len(items)):
                angle = math.radians((rotate * i) - 90)
                x = (magnitude * math.cos(angle)) + w
                y = (magnitude * math.sin(angle)) + h
                items[i].place(x, y)
                self.activities.add(items[i])
            
        self.finger = Finger()
        self.Cursor = pygame.sprite.RenderPlain((self.finger))
        
        self.pos = None
        
    def setup_background(self):
        self.background.fill((200,200,200))
        
    def handle_events(self):
        event = pygame.event.wait()
        if event.type == QUIT:
            self.quit = True
            return
        elif event.type == KEYUP:
            if event.key == K_F4 and KMOD_ALT & event.mod:
                self.quit = True
                return
        self.pos = pygame.mouse.get_pos()
        if self.pos != self.mprev:
            self.changed = True
            
    def on_change(self):
        self.Cursor.update()        

        for x in pygame.sprite.spritecollide(self.finger, self.activities, False):
            x.run(self.screen)
            pygame.mouse.set_pos(self.screen.get_width() / 2, self.screen.get_height() / 2)
            break
        self.activities.draw(self.screen)
        self.Cursor.draw(self.screen)
    
        self.mprev = self.pos
